﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Pahan.McuFusion.Common;

namespace Pahan.McuFusion.Files
{
    public class BinaryFileReader : IFileReader
    {
        public BinaryFileReader(Stream stream)
        {
            Assert.NotNull(stream, "stream");
            if (!stream.CanRead)
            {
                throw new ArgumentException("The stream cannot be read.", "stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream cannot be seeked.", "stream");
            }

            this.stream = stream;
        }
        
        #region Implementation of IDisposable

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            stream.Close();
        }

        #endregion

        #region Implementation of IFileReader

        public byte DefaultValue
        {
            get;
            set;
        }

        public IList<byte> ReadAll()
        {
            var buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }

        #endregion

        private readonly Stream stream;
    }
}
